package com.shop.service.bclass;

import com.shop.dao.BaseDao;
import com.shop.dao.bclass.ClassDaoImpl;
import com.shop.dao.bclass.IClassDao;
import com.shop.pajo.BClass;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class ClassServiceImpl implements IClassService{

    private IClassDao classDao = null;
    public ClassServiceImpl(){
        classDao = new ClassDaoImpl();
    }

    @Override
    public List<BClass> query(String queryCode) {
        Connection connection = null;
        List<BClass> bClassList = null;

        try {
            connection = BaseDao.getConnection();
            bClassList = classDao.query(connection,queryCode);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection,null,null);
        }
        return bClassList;
    }

    @Override
    public BClass queryByAll(String queryCode, String queryClass) {
        Connection connection = BaseDao.getConnection();
        BClass bClass = null;
        try {
            bClass = classDao.queryByAll(connection,queryCode,queryClass);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection,null,null);
        }
        return bClass;
    }

    @Override
    public int add(BClass bClass) {
        Connection connection = BaseDao.getConnection();
        int addNum = 0;
        try {
            connection.setAutoCommit(false);
            addNum = classDao.add(connection,bClass);
            connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        } finally {
            BaseDao.closeResource(connection,null,null);
        }
        return addNum;
    }

    @Override
    public int delete(String id, String bClass) {
        Connection connection = BaseDao.getConnection();
        int deleteNum = 0;
        try {
            connection.setAutoCommit(false);
            deleteNum = classDao.deleteClassById(connection,id,bClass);
            connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        } finally {
            BaseDao.closeResource(connection,null,null);
        }
        return deleteNum;
    }
}
